﻿//====================================================================================================================
//Copyright (c) 2011 IdeaBlade
//====================================================================================================================
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
//and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//====================================================================================================================
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of 
//the Software.
//====================================================================================================================
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
//WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 
//OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
//OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
//====================================================================================================================

using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using Caliburn.Micro;
using HelloWorld.Events;
using HelloWorld.Repository;
using IdeaBlade.Application.Framework.Core.Authentication;
using IdeaBlade.Application.Framework.Core.Persistence;
using IdeaBlade.Application.Framework.Core.ViewModel;
using IdeaBlade.EntityModel;

namespace HelloWorld.ViewModel
{
    [Export(typeof(IMainScreen))]
    public class OrderScreenViewModel : Screen, IMainScreen, IDiscoverableViewModel
    {
        private readonly IOrderManagementRepository _repository;
        private readonly IEventAggregator _eventAggregator;
        private readonly IAuthenticationService _authenticationService;
        private Customer _currentCustomer;
        private Order _currentOrder;

        private BindableCollection<Customer> _customers;
        private bool _isBusy;

        [ImportingConstructor]
        public OrderScreenViewModel(IOrderManagementRepository repository, IEventAggregator eventAggregator,
                                    IAuthenticationService authenticationService)
        {
            _repository = repository;
            _eventAggregator = eventAggregator;
            _authenticationService = authenticationService;
        }

        public BindableCollection<Customer> Customers
        {
            get { return _customers; }
            private set
            {
                _customers = value;
                NotifyOfPropertyChange(() => Customers);
            }
        }

        public Customer CurrentCustomer
        {
            get { return _currentCustomer; }
            set
            {
                _currentCustomer = value;
                NotifyOfPropertyChange(() => CurrentCustomer);
            }
        }

        public Order CurrentOrder
        {
            get { return _currentOrder; }
            set
            {
                _currentOrder = value;
                NotifyOfPropertyChange(() => CanShowOrderDetail);
                NotifyOfPropertyChange(() => CurrentOrder);
            }
        }

        public string CurrentUser
        {
            get { return "You are currently logged in as: " + _authenticationService.Principal.Identity.Name; }
        }

        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                NotifyOfPropertyChange(() => IsBusy);
            }
        }

        public bool CanShowOrderDetail
        {
            get { return CurrentOrder != null; }
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();
            Start();
        }

        public OrderScreenViewModel Start()
        {
            GetCustomers();
            return this;
        }

        public void GetCustomers()
        {
            IsBusy = true;
            _repository.GetCustomers(
                results =>
                {
                    Customers = new BindableCollection<Customer>(results);
                    CurrentCustomer = Customers.FirstOrDefault();
                    IsBusy = false;
                });
        }

        public void ShowOrderDetail()
        {
            _eventAggregator.Publish(new EditOrderMessage(CurrentOrder));
        }
    }
}